package com.ehotting.edsta.mqbd.driverAdapt.service.impl;

import com.alibaba.fastjson.JSON;
import com.ehotting.eaf.core.utils.OkHttpUtils;
import com.ehotting.eaf.core.utils.ip.IpUtils;
import com.ehotting.edsta.mqbd.driverAdapt.service.IDriverAdaptService;
import com.ehotting.mqbd.api.bean.CommonRequest;
import com.ehotting.mqbd.api.bean.CommonResponse;
import com.ehotting.mqbd.api.bean.solver.SolverInfoBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.Map;

@Slf4j
@Service
public class DriverAdaptServiceImpl implements IDriverAdaptService {

    @Override
    public CommonResponse rePostService(CommonRequest request,SolverInfoBean solverInfo,String authorization) {
        OkHttpUtils okHttpUtils = packOkHttpUtils(request,solverInfo,authorization);
        log.info("Start rePostService.");
        String sync = null;
        long startTime = System.currentTimeMillis();
        if ("post".equalsIgnoreCase(request.getRequestMethod())) {
            String contentType = request.getHeaders().get("contentType");
            if ("multipart/form-data".equalsIgnoreCase(contentType)) {
                // 文件上传
                String string = JSON.toJSONString(request.getRequestBody());
                Map<String, String> map = JSON.parseObject(string, Map.class);

                String fileContent = new String(Base64.getDecoder().decode(map.get("fileContent")));
                map.put("fileContent", fileContent);
                sync = okHttpUtils.addParam(map).uploadFile();
            } else {
            /*else if(url.contains("files/download")){
                // 文件下载
                log.info("文件下载>>>>");
                sync = okHttpUtils.addBody(request.getRequestBody()).postBody(true).asyncDownload();
            }*/
                sync = okHttpUtils.addBody(request.getRequestBody()).postBody(true).sync();
            }
        }
        log.info("CostTime:{}",(System.currentTimeMillis()-startTime));
        log.info("Finish rePostService.");

        return packCommonResponse(sync, request.getWebInfo());
    }





    @Override
    public CommonResponse rePostUploadService(CommonRequest request,SolverInfoBean solverInfo,String authorization) {
        log.info("Start rePostUploadService.");
        long startTime = System.currentTimeMillis();
        OkHttpUtils okHttpUtils = packOkHttpUtils(request,solverInfo,authorization);

        String contentType = request.getHeaders().get("contentType");
        String url = request.getRequestUrl();
        String responseContent = null;
        if ("multipart/form-data".equalsIgnoreCase(contentType)) {
            // 文件上传
            String string = JSON.toJSONString(request.getRequestBody());
            if (url.contains("files/uploadFolder")){
                // 如果是上传文件夹
                responseContent = okHttpUtils.uploadDir(string);
            }else {
                // 单个文件上传
                Map<String, String> map = JSON.parseObject(string, Map.class);
                String fileContent = new String(Base64.getDecoder().decode(map.get("fileContent")));
                map.put("fileContent", fileContent);
                responseContent = okHttpUtils.addParam(map).uploadFile();
            }
        }else {
            responseContent = okHttpUtils.addBody(request.getRequestBody()).postBody(true).sync();
        }

        log.info("CostTime:{}",(System.currentTimeMillis()-startTime));
        log.info("Finish rePostUploadService.");
        return packCommonResponse(responseContent, request.getWebInfo());
    }

    private CommonResponse packCommonResponse(String responseContent, Map<String, Object> webInfo) {
        log.info("responseContent :{}", responseContent);
        Map<String,Object> customResponse = null;
        if (responseContent != null) {
            customResponse = JSON.parseObject(responseContent, Map.class);
        }
        CommonResponse commonResponse = new CommonResponse();
        commonResponse.setWebInfo(webInfo);
        commonResponse.setResponse(customResponse);
        return commonResponse;
    }

    private OkHttpUtils packOkHttpUtils(CommonRequest request, SolverInfoBean solverInfo,String authorization) {
        String requestUrl = request.getRequestUrl();
        log.info("solverInfo.toSplitString():{}",solverInfo.toStringIpAndPort());
        String newUrl = IpUtils.replaceIpPort(requestUrl, solverInfo.toStringIpAndPort());
        log.info("newUrl:{}",newUrl);
        return OkHttpUtils.builder().url(newUrl)
                .addHeader(request.getHeaders())
                .addHeader("authorization",authorization);
    }

}
